import sys
from PySide6.QtCore import QDate, Qt, QSize
from PySide6.QtGui import QFont, QIcon
from PySide6.QtWidgets import (
    QApplication, QHBoxLayout, QLabel,
    QSizePolicy,
    QSpacerItem, QToolButton, QVBoxLayout, QWidget, QStackedWidget, QCheckBox
)
from qfluentwidgets import PushButton
from models import DataReviewTable, DataSelectTable, ResultView, TopBar



class BreadcrumbNavigation(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.mainLayout = QVBoxLayout(self)
        self.mainLayout.setContentsMargins(0, 0, 0, 0)

        # 导航栏
        self.breadcrumbs = QWidget()
        self.breadcrumbs.setFixedHeight(50)
        self.breadcrumb_layout = QHBoxLayout(self.breadcrumbs)
        self.breadcrumb_layout.setAlignment(Qt.AlignLeft)

        self.btn_select = PushButton()
        self.btn_select.setText("数据选择")
        # self.btn_step1.setFont(font)  # 设置按钮字体
        self.btn_select.setFixedHeight(40)  # 适当调整按钮的大小
        self.btn_select.setFixedWidth(120)

        self.btn_review = PushButton()
        self.btn_review.setText("数据预览")
        # self.btn_step2.setFont(font)
        self.btn_review.setFixedHeight(40)
        self.btn_review.setFixedWidth(120)

        self.btn_result = PushButton()
        self.btn_result.setText("结果检查")
        # self.btn_step3.setFont(font)
        self.btn_result.setFixedHeight(40)
        self.btn_result.setFixedWidth(120)

        self.btn_select.clicked.connect(lambda: self.navigate_to(0))
        self.btn_review.clicked.connect(lambda: self.navigate_to(1))
        self.btn_result.clicked.connect(lambda: self.navigate_to(2))

        self.breadcrumb_layout.addWidget(self.btn_select)
        self.breadcrumb_layout.addWidget(QLabel("-->"))
        self.breadcrumb_layout.addWidget(self.btn_review)
        self.breadcrumb_layout.addWidget(QLabel("-->"))
        self.breadcrumb_layout.addWidget(self.btn_result)

        self.breadcrumbs.setContentsMargins(0, 0, 0, 0)  # 去除边距

        self.mainLayout.addWidget(self.breadcrumbs)

        # 堆栈页面
        self.stacked_widget = QStackedWidget(self)

        # 第一步：数据选择页面
        self.page_dataSelect = QWidget()
        page_select_layout = QVBoxLayout(self.page_dataSelect)

        self.dataSelectTable = DataSelectTable.DataSelectTable()  # 自定义数据选择表格
        self.dataSelectTable.setObjectName("dataSelectTable")
        page_select_layout.addWidget(self.dataSelectTable)

        self.page_dataSelect_bottomBar = QWidget()
        self.page_dataSelect_bottomBar.setFixedHeight(80)
        dataSelectBottomBarLayout = QHBoxLayout(self.page_dataSelect_bottomBar)
        selectAllBtn = PushButton("全选")
        notSelectAllBtn = PushButton("全不选")
        DataStatsInfoBtn = PushButton("数据统计信息")
        dataSelect_next_btn = PushButton("下一步")
        dataSelect_next_btn.clicked.connect(lambda: self.navigate_to(1))
        # 连接全选与全不选
        selectAllBtn.clicked.connect(self.dataSelectTable.selectAll)
        notSelectAllBtn.clicked.connect(self.dataSelectTable.deselectAll)

        dataSelectBottomBarLayout.addWidget(selectAllBtn)
        dataSelectBottomBarLayout.addWidget(notSelectAllBtn)
        dataSelectBottomBarLayout.addWidget(DataStatsInfoBtn)
        dataSelectBottomBarLayout.addWidget(dataSelect_next_btn)
        page_select_layout.addWidget(self.page_dataSelect_bottomBar)
        self.stacked_widget.addWidget(self.page_dataSelect)

        # 第二步：数据复检页面
        self.page_dataReview = QWidget()
        page_dataReview_layout = QVBoxLayout(self.page_dataReview)

        review_table = DataReviewTable.DataReviewTable()  # 自定义表格带有checkbox，丑
        page_dataReview_layout.addWidget(review_table)

        data_review_bottomBar = QWidget()
        data_review_bottomBar.setFixedHeight(150)
        data_review_bottomBarLayout = QHBoxLayout(data_review_bottomBar)
        smartCheckBtn = PushButton("一键智能检查")
        dataAnalyseBtn = PushButton("一键分析")
        runningStatusBtn = PushButton("运行状态显示")
        dataReview_prev_btn = PushButton("上一步")
        dataReview_prev_btn.clicked.connect(lambda: self.navigate_to(0))
        dataReview_next_btn = PushButton("下一步")
        dataReview_next_btn.clicked.connect(lambda: self.navigate_to(2))
        data_review_bottomBarLayout.addWidget(smartCheckBtn)
        data_review_bottomBarLayout.addWidget(dataAnalyseBtn)
        data_review_bottomBarLayout.addWidget(runningStatusBtn)
        data_review_bottomBarLayout.addWidget(dataReview_prev_btn)
        data_review_bottomBarLayout.addWidget(dataReview_next_btn)
        page_dataReview_layout.addWidget(data_review_bottomBar)
        self.stacked_widget.addWidget(self.page_dataReview)

        # 第三步：结果检查页面
        self.page_ResultCheck = QWidget()
        resultCheck_layout = QVBoxLayout(self.page_ResultCheck)
        resultCheck_layout.setContentsMargins(0, 0, 0, 0)

        resultTable = ResultView.ResultView()  # 自定义表格，很丑
        resultCheck_layout.addWidget(resultTable)

        resultCheck_bottomBar = QWidget()
        resultCheck_bottomBar.setFixedHeight(60)
        resultCheck_bottomBarLayout = QHBoxLayout(resultCheck_bottomBar)
        archiveBtn = PushButton("存档到报告")
        paraSetBtn = PushButton("参数设置")
        discardResBtn = PushButton("放弃结果")
        exportResBtn = PushButton("导出结果")
        resultCheck_prev_btn = PushButton("上一步")
        resultCheck_prev_btn.clicked.connect(lambda: self.navigate_to(1))
        resultCheck_bottomBarLayout.addWidget(archiveBtn)
        resultCheck_bottomBarLayout.addWidget(paraSetBtn)
        resultCheck_bottomBarLayout.addWidget(discardResBtn)
        resultCheck_bottomBarLayout.addWidget(exportResBtn)
        resultCheck_bottomBarLayout.addWidget(resultCheck_prev_btn)
        resultCheck_layout.addWidget(resultCheck_bottomBar)
        self.stacked_widget.addWidget(self.page_ResultCheck)

        self.mainLayout.addWidget(self.stacked_widget)

        self.setLayout(self.mainLayout)
        self.navigate_to(0)


    # 跳转页面
    def navigate_to(self, index):
        self.stacked_widget.setCurrentIndex(index)
        self.update_breadcrumb(index)

    def update_breadcrumb(self, index):
        # 更新面包屑导航的外观，高亮当前步骤
        buttons = [self.btn_select, self.btn_review, self.btn_result]
        for i, btn in enumerate(buttons):
            if i == index:
                btn.setStyleSheet("background-color: rgb(0, 163, 168); color: white;font-size: 18px;")
            else:
                btn.setStyleSheet("font-weight: normal; color: black;font-size: 18px;")


class DataAnalyse(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMinimumSize(QSize(900, 700))
        self.setupUi()

    def setupUi(self):
        self.mainlayout = QVBoxLayout(self)
        self.mainlayout.setSpacing(0)

        self.topBar = TopBar.TopBar()
        self.mainlayout.addWidget(self.topBar)

        self.breadcrumb = BreadcrumbNavigation()
        self.mainlayout.addWidget(self.breadcrumb)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = DataAnalyse()
    window.show()
    sys.exit(app.exec())
